<?php

class ControllerModuleNews extends Controller {

    public function index() {
        //Load the language file for this module - catalog/language/module/news.php
        $this->language->load('module/news');
        //Load any required model files - catalog/product is a common one, or you can make your own DB access
        //methods in catalog/model/module/news.php
        $this->load->model('module/news');
        $this->load->model('tool/image');

        //Example functionality: pull through customer firstnames and make them available to the view.
        $this->data['customers'] = $this->model_module_news->getCustomerFirstnames();
        
        $this->document->setTitle($this->language->get('heading_title'));
        $this->data['text_readmore'] = $this->language->get('text_readmore');
        $this->data['heading_title'] = $this->language->get('heading_title');
        
        $id = 1;
        $category = $this->model_module_news->getCategory($id);

        //Get the title from the language file
        $this->data['heading_title'] = $category['category_title'];

        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/home'),
            'separator' => false
        );

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('heading_title'),
            'href' => $this->url->link('module/news'),
            'separator' => $this->language->get('text_separator')
        );


        $limit = 10;
        $posts = $this->model_module_news->getPostsFromCategory($id, $limit);

        foreach ($posts as $post) {
            if ($post['status'] == 1) {
                $this->data['posts'][] = array(
                    'post_id' => $post['post_id'],
                    'post_title' => $post['post_title'],
                    'post_description' => mb_substr(strip_tags(html_entity_decode($post['post_content'])), 0, 100),
                    'post_img' => $this->model_tool_image->resize($post['post_img'], 170, 100),
                    'post_date' => $post['post_date'],
                    'post_link' => $this->url->link('module/news/post', 'path=' . $post['post_id'])
                );
            }
        }

        // Advertisements.
        $this->load->model("design/banner");
        $ad_infos = $this->model_design_banner->getBanner(8);
        $ads = array();

        foreach ($ad_infos as $ad_info) {
            $url = "";
            if (isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] == 'on') || ($this->request->server['HTTPS'] == '1'))) {
                $url = $this->config->get('config_ssl') . 'image/';
                $root = $this->config->get('config_ssl');
            } else {
                $url = $this->config->get('config_url') . 'image/';
            }

            $ads[] = array(
                'image' => $url . $ad_info['image']
            );
        }
        $this->data['ads'] = $ads;
        // End Advertisements.

        //Choose which template to display this module with
        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/news.tpl')) {
            $this->template = $this->config->get('config_template') . '/template/module/news.tpl';
        } else {
            $this->template = 'default/template/module/news.tpl';
        }

        $this->children = array(
            'common/column_left',
            'common/column_right',
            'common/content_top',
            'common/content_bottom',
            'common/footer',
            'common/header'
        );

        $this->response->setOutput($this->render());
    }

    public function post() {
        $this->language->load('module/news');
        $this->load->model('module/news');
        $this->load->model('tool/image');

        $this->document->setTitle($this->language->get('heading_title'));
        $this->data['text_related'] = $this->language->get('text_related');
        $this->data['heading_title'] = $this->language->get('heading_title');

        if (isset($this->request->get['path'])) {
            $post_id = $this->request->get['path'];
            $post = $this->model_module_news->getPost($post_id);
            $category_id = $post['category_id'];
            $this->data['heading_title'] = $post['post_title'];
            $related_posts = $this->model_module_news->getPostsFromCategory($category_id, 5);
            foreach ($related_posts as $p) {
                $this->data['related_posts'][] = array(
                    'post_title' => $p['post_title'],
                    'post_link' => $this->url->link('module/news/post', 'path=' . $p['post_id'])
                );
            }

            $this->data['post'] = array(
                'post_id' => $post['post_id'],
                'post_title' => $post['post_title'],
                'post_content' => html_entity_decode($post['post_content']),
                'post_date' => $post['post_date'],
                'post_link' => $this->url->link('module/news/post', 'path=' . $post['post_id'])
            );
            
          
            $this->data['breadcrumbs'] = array();

            $this->data['breadcrumbs'][] = array(
                'text' => $this->language->get('text_home'),
                'href' => $this->url->link('common/home'),
                'separator' => false
            );
            $this->data['breadcrumbs'][] = array(
                'text' => $this->language->get('heading_title'),
                'href' => $this->url->link('module/news'),
                'separator' => $this->language->get('text_separator')
            );

            if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/module/news_post.tpl')) {
                $this->template = $this->config->get('config_template') . '/template/module/news_post.tpl';
            } else {
                $this->template = 'default/template/module/news_post.tpl';
            }

            $this->children = array(
                'common/column_left',
                'common/column_right',
                'common/content_top',
                'common/content_bottom',
                'common/footer',
                'common/header'
            );

            // Advertisements.
            $this->load->model("design/banner");
            $ad_infos = $this->model_design_banner->getBanner(8);
            $ads = array();

            foreach ($ad_infos as $ad_info) {
                $url = "";
                if (isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] == 'on') || ($this->request->server['HTTPS'] == '1'))) {
                    $url = $this->config->get('config_ssl') . 'image/';
                    $root = $this->config->get('config_ssl');
                } else {
                    $url = $this->config->get('config_url') . 'image/';
                }

                $ads[] = array(
                    'image' => $url . $ad_info['image']
                );
            }
            $this->data['ads'] = $ads;
            // End Advertisements.

            $this->response->setOutput($this->render());
        } else {
            //display not found
        }
    }

}

?>